const express = require('express');
const https = require('https');
const fs = require('fs');
const path = require('path');

const app = express();

// 读取 SSL 证书和私钥
const privateKey = fs.readFileSync(path.join(__dirname, 'public/certs/your-domain.com-key.pem'), 'utf8');
const certificate = fs.readFileSync(path.join(__dirname, 'public/certs/your-domain.com.pem'), 'utf8');
const credentials = { key: privateKey, cert: certificate };

// 使用 http-server 提供的静态资源服务
const server = https.createServer(credentials, app);

// 在 express 中设置 cache-control 为 no-cache (针对字体文件)
app.use((req, res, next) => {
  const extname = path.extname(req.url);
  if (extname === '.woff' || extname === '.woff2' || extname === '.ttf') {
    res.setHeader('Cache-Control', 'no-cache, must-revalidate');
    // res.setHeader('Cache-Control', 'no-store');
  }
  next();
});

// 使用 express 监听本地端口
app.use(express.static(path.join(__dirname, 'public')));

// 运行服务器
const port = 443;
server.listen(port, () => {
  console.log(`Server running at https://localhost:${port}`);
});
